package com.huluobo.high3demo.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

/**
 * 贝塞尔曲线 带动画 飘心view
 * Created by LC on 2023/9/27.
 */
public class BezierLineAnimView extends RelativeLayout {
    public void setResId(int[] resId) {
        this.resId = resId;
    }

    private int[] resId;

    public BezierLineAnimView(Context context) {
        super(context);
    }

    public BezierLineAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addFavor() {
        //1.创建出一个ImageView添加到当前的布局上
        ImageView animView = new ImageView(getContext());
        int index = (int) (Math.random() * resId.length);//取一个随机数的整数
        animView.setImageResource(resId[index]);
        LayoutParams params = new LayoutParams(200, 200);
        animView.setLayoutParams(params);
        addView(animView);

        //2.定义起始点和控制点(三阶贝塞尔曲线)
        int width = getMeasuredWidth();//拿到当前Layout的宽
        int height = getMeasuredHeight();//拿到当前Layout的高
        float offset = 100;//图片一半的大小

        //定义起始坐标点
        PointF P0 = new PointF(width / 2.0f - offset, height);
        PointF P3 = new PointF(width / 2.0f - offset, 0);

        //定义控制点
        PointF P1 = new PointF((float) (Math.random() * width - offset), height / 3.0f * 2 - offset);
        PointF P2 = new PointF((float) (Math.random() * width - offset), height / 3.0f - offset);

        //3.定义属性动画
        ValueAnimator animator = ValueAnimator.ofObject(new BezierTypeEvaluator(P1, P2), P0, P3);
        animator.setDuration(1200);
        animator.setInterpolator(new AccelerateInterpolator());//设置加速插值器
        //动画的更新监听
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                //更新动画图片坐标值
                animView.setX(pointF.x);
                animView.setY(pointF.y);
                //设置透明度
                float fraction = animator.getAnimatedFraction();
                animView.setAlpha(1 - fraction);
            }
        });
        //动画的完成监听
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(animView);
            }
        });
        //4.启动动画
        animator.start();
    }


    class BezierTypeEvaluator implements TypeEvaluator<PointF> {

        PointF P1, P2;

        public BezierTypeEvaluator(PointF p1, PointF p2) {
            P1 = p1;
            P2 = p2;
        }

        @Override
        public PointF evaluate(float t, PointF P0, PointF P3) {
            PointF P = new PointF();
            //三阶贝塞尔曲线
            //计算x轴
            P.x = P0.x * (1 - t) * (1 - t) * (1 - t)
                    + 3 * P1.x * t * (1 - t) * (1 - t)
                    + 3 * P2.x * t * t * (1 - t)
                    + P3.x * t * t * t;

            //计算y轴
            P.y = P0.y * (1 - t) * (1 - t) * (1 - t)
                    + 3 * P1.y * t * (1 - t) * (1 - t)
                    + 3 * P2.y * t * t * (1 - t)
                    + P3.y * t * t * t;
//              四阶贝塞尔曲线
//            P.x = P0.x * (1 - t) * (1 - t) * (1 - t) * (1 - t) //起点
//                    + 4 * P1.x * t * (1 - t) * (1 - t) * (1 - t)//控制点1
//                    + 4 * P2.x * t * t * (1 - t) * (1 - t)//控制点2
//                    + 4 * P3.x * t * t * t * (1 - t)//控制点3
//                    + 4 * P4.x * t * t * t * t;//终点
            //二阶贝塞尔曲线
//            P.x = P0.x * (1 - t) * (1 - t)
//                    + 2 * P1.x * t * (1 - t)
//                    + 2 * P2.x * t * t;
            //一阶贝塞尔曲线
//            P.x = P0.x * (1 - t)
//                    + 1 * P1.x * t;
            return P;
        }
    }
}
